namespace Brjnk.GpxExplorer.Workspace.BitmapExport
{
    using System;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Brjnk.Common;

    /// <summary>
    /// </summary>
    public class UserLabel : ArrowLabel
    {
        public UserLabel(Page page, LabelStyle labelStyle, ArrowStyle arrowStyle, UserLabelStyle userLabelStyle)
            : base(page, labelStyle, arrowStyle)
        {
            if ((object)userLabelStyle == null) throw new ArgumentNullException("userLabelStyle");
            this.UserLabelStyle = userLabelStyle;
            CreateCommands();
        }

        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; OnPropertyChanged("Text"); }
        }

        private ImageSource image;

        public ImageSource Image
        {
            get { return image; }
            set { image = value; OnPropertyChanged("Image"); }
        }

        private double imageSize;

        public double ImageSize
        {
            get { return imageSize; }
            set { imageSize = value; OnPropertyChanged("ImageSize"); }
        }

        private UserLabelStyle userLabelStyle;

        public UserLabelStyle UserLabelStyle
        {
            get { return userLabelStyle; }
            private set { userLabelStyle = value; OnPropertyChanged("UserLabelStyle"); }
        }

        private void CreateCommands()
        {
            RemoveImageCommand = new GenericCommand(o => RemoveImage((UserLabel)o));
            ChooseImageCommand = new GenericCommand(o => ChooseImage((UserLabel)o));
        }

        private void RemoveImage(UserLabel label)
        {
            label.Image = null;
        }

        private void ChooseImage(UserLabel label)
        {
            var ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Multiselect = false;
            if (ofd.ShowDialog() ?? false)
            {
                label.Image = new BitmapImage(new Uri(ofd.FileName));
            }
        }

        public GenericCommand RemoveImageCommand { get; private set; }

        public GenericCommand ChooseImageCommand { get; private set; }
    }

    public enum UserLabelLayout
    {
        ImageAndText,
        TextAndImage
    }
}